#!/usr/bin/env python
# coding: utf-8
import pandas as pd

#平均值特征构造
def add_average_features(df):
    columns_to_average = [
        ('m1_owe_fee', 'm2_owe_fee', 'm3_owe_fee', 'mean_owe_fee'),  # 用户三个月的缴费
        ('m1_calling_cnt', 'm2_calling_cnt', 'm3_calling_cnt', 'mean_calling_cnt'),  # 用户三个月的主叫次数
        ('m1_comm_days', 'm2_comm_days', 'm3_comm_days', 'mean_comm_days'),  # 用户三个月通信天数
        ('m1_hot_app_flow', 'm2_hot_app_flow', 'm3_hot_app_flow', 'mean_hot_app_flow')  # 用户三个月的热门APP使用流量
    ]
    for col1, col2, col3, new_col in columns_to_average:
        df[new_col] = df[[col1, col2, col3]].mean(axis=1)  # 计算每列的三个月平均值
    return df

#时间相关特征构造
def add_time_features(df):
    df['enter_month'] = df['innet_months'] % 12  # 用户入网月份
    df['age_innet'] = df['age'] - (df['innet_months'] / 12)  # 用户入网时的年龄
    return df

#通话特征构造
def add_call_features(df):
    # 近三个月用户主动呼叫通话次数
    df['contact_avg_tocall_cnt_m3'] = df['contact_avg_call_cnt_m3'] - df['contact_avg_called_cnt_m3']  
     # 被呼叫比率
    df['call_rate'] = df['contact_avg_called_cnt_m3'] / (df['contact_avg_call_cnt_m3'] + 1e-9)  
    # 主动呼叫比率
    df['tocall_rate'] = df['contact_avg_tocall_cnt_m3'] / (df['contact_avg_call_cnt_m3'] + 1e-9)  
    # 国内漫游呼叫比率
    df['domestic_roam_call_rate'] = df['domestic_roam_call_count_m3'] / (df['contact_avg_call_cnt_m3'] + 1e-9)  
    # 宽带相关呼叫次数
    df['bw_contact_call_count_m3'] = df['contact_avg_call_cnt_m3'] / (df['bw_contact_count_m3'] + 1e-9) 
    # 密集区域通话特征（交往圈规模*规模变化率）
    df['dense_sphere'] = df['dense_sphere_num'] * df['dense_sphere_rate']  
    return df

#用户表现特征构造
def add_interaction_features(df):
     # 用户积分余额的月均值：用户当前的积分余额除以入网月份数
    df['avg_curmon_acum_qty'] = df['curmon_acum_qty'] / df['innet_months']  
    # 积分余额增长率：当前积分余额与过去6个月积分余额的平均值的变化率
    df['acum_rate'] = (df['curmon_acum_qty'] - df['acum_all_total_6m_mean']) / (df['acum_all_total_6m_mean'] + 1e-9)  
    # 近三个月的缴费保持情况： 增加的缴费月数 - 减少的缴费月数
    df['pay_fee_keep_3m'] = 3 - df['pay_fee_add_3m'] - df['pay_fee_dec_3m']  
    # 家庭宽带与虚拟专网（VPMN）标识的交互特征：反映用户同时订购家庭宽带和虚拟专网服务的情况
    df['family_vpmn_interaction'] = df['family_brodbd_flag'] * df['vpmn_flag'] 
    # 积分余额的月均值：当前的积分余额除以用户的入网时长
    df['avg_acum_qty'] = df['curmon_acum_qty'] / (df['innet_months'] + 1e-9)
    # 用户星级月平均值：用户的星级评分除以入网月份数，反映长期星级表现
    df['avg_star_level'] = df['star_level'] / (df['innet_months'] + 1e-9)
    return df

#变化率特征构造
def add_growth_rate_features(df):
    for metric in ['hot_app_flow', 'comm_days', 'calling_cnt', 'owe_fee']:#热门APP使用流量、通信天数、主叫次数、缴费金额
        for i,j,k in [[1,1,2],[2,1,3],[3,2,3]]:
            df[f'{metric}_growth_rate{i}'] = (df[f'm{k}_{metric}'] - df[f'm{j}_{metric}']) / (df[f'm{j}_{metric}'] + 1e-9)  #变化率
            df[f'{metric}_change{i}'] = df[f'm{k}_{metric}'] - df[f'm{j}_{metric}']  #变化量
    return df

def preprocess_data(data_path):
    data = pd.read_csv(data_path)

    data = add_average_features(data)
    data = add_time_features(data)
    data = add_call_features(data)
    data = add_interaction_features(data)
    data = add_growth_rate_features(data)

    return data

if __name__ == "__main__":
    data_path = '../init_data/raw/测试集A/train.csv'
    train_data = preprocess_data(data_path)
    print("Train data shape:", train_data.shape)

